package io.github.jast90.internship.controller;

import io.github.jast90.common.form.CommonPageRequestBody;
import io.github.jast90.internship.entity.Announcement;
import io.github.jast90.internship.service.AnnouncementService;
import io.github.jast90.internship.util.SessionUtil;
import io.github.jast90.mybatis.pagination.domain.Page;
import io.github.jast90.mybatis.pagination.domain.PageRequest;
import io.github.jast90.web.common.controller.BaseController;
import io.github.jast90.web.common.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by jast90 on 2021/3/8
 */
@RestController
public class AnnouncementRestController extends BaseController<AnnouncementService, Announcement> {

    @Autowired
    private SessionUtil sessionUtil;

    @PostMapping(value = "announcement/page")
    @Override
    public CommonResult<Page<Announcement>> queryPage(@RequestBody CommonPageRequestBody<PageRequest, Announcement> requestBody) {

        return service.queryPage(requestBody.getPageRequest(),requestBody.getDomain());
    }

    @GetMapping("announcement/{id}")
    @Override
    public CommonResult<Announcement> queryById(@PathVariable("id") long id) {
        return service.queryById(id);
    }

    @PostMapping("announcement")
    @Override
    public CommonResult<Announcement> add(@RequestBody Announcement entity) {
        entity.setCreateUserId(sessionUtil.getUid());
        return service.add(entity);
    }

    @PutMapping("announcement")
    @Override
    public CommonResult<Announcement> update(@RequestBody Announcement entity) {
        entity.setCreateUserId(sessionUtil.getUid());
        return service.updateById(entity);
    }

    @DeleteMapping("announcement/{id}")
    @Override
    public CommonResult<Announcement> delete(@PathVariable("id") long id) {
        return service.deleteById(id);
    }
}
